package com.longdaji.decoration.ui.splash;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.AdBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.AbortableFuture;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.LoginInfo;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class SplashPresenter extends RxPresenter<SplashContract.View> implements SplashContract.Presenter {
    private final static String TAG = SplashPresenter.class.getSimpleName();
    private DataManager mDataManager;
    private AbortableFuture<LoginInfo> loginRequest;

    @Inject
    public SplashPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void getAd() {
        addSubscribe(mDataManager.getAd()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<AdBean>handleResult())
                .subscribeWith(new CommonSubscriber<AdBean>() {
                    @Override
                    public void onSuccess(AdBean adBean) {
                        Log.d(TAG, "AdBean is" + adBean);
                        if (mView != null) {
                            if (adBean != null && adBean.getStartPage() != null) {
                                mView.showAd(adBean.getStartPage());
                            } else {
                                mView.showNoAd();
                            }

                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showNoAd();
                        }
                    }
                }));
    }

    @Override
    public void autoLogin(String username, String password) {
        addSubscribe(mDataManager.doPasswordLoginApi(username, password, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Account>handleResult())
                .subscribeWith(new CommonSubscriber<Account>() {
                    @Override
                    public void onSuccess(final Account account) {
                        LogUtil.d(TAG, "login succeed : msg : " + account.toString());
                        if (account == null) {
                            return;
                        }
                        if (loginRequest != null) {
                            loginRequest.abort();
                            loginRequest = null;
                        }
                        loginRequest = NimUIKit.login(new LoginInfo(account.getUserid(), account.getToken()), new RequestCallback<LoginInfo>() {
                            @Override
                            public void onSuccess(LoginInfo param) {
                                LogUtil.i(TAG, "login success");
                                loginRequest = null;
                                if (mView != null) {
                                    mView.goMainByUsername(account);
                                }

                            }

                            @Override
                            public void onFailed(int code) {
                                loginRequest = null;
                                if (mView != null) {
                                    mView.showNimLoginFail();
                                }
                            }

                            @Override
                            public void onException(Throwable exception) {
                                loginRequest = null;
                                if (mView != null) {
                                    mView.showNimLoginFail();
                                }
                            }
                        });

                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(TAG, "login failed : msg : " + msg);
                        if (mView != null) {
                            mView.showNimLoginFail();
                        }
                    }
                })
        );
    }
}
